package com.wt.admin.controller.sys;

import com.aizuda.easy.security.domain.Rep;
import com.aizuda.easy.security.util.IPUtil;
import com.aizuda.easy.security.util.LocalUtil;
import com.wt.admin.config.aspect.annotation.LogAno;
import com.wt.admin.domain.dto.sys.SysUserInfoUpdateDTO;
import com.wt.admin.domain.dto.sys.SysUserLoginDTO;
import com.wt.admin.domain.dto.sys.SysUserPasswordDTO;
import com.wt.admin.domain.entity.sys.SysUserEntity;
import com.wt.admin.domain.vo.SysUserListVO;
import com.wt.admin.domain.vo.UserVO;
import com.wt.admin.service.sys.SysProxyService;
import com.wt.admin.util.PageUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;


@RestController
@RequestMapping("user")
public class SysUserController {

    @Resource
    private SysProxyService sysProxyService;

    @LogAno(name = "登录")
    @PostMapping("login")
    public Rep<UserVO> userLogin(@RequestBody SysUserLoginDTO data, HttpServletRequest request){
        String clientIP = IPUtil.getIp(request);
        data.setClientIP(clientIP);
        return Rep.ok(sysProxyService.login(data));
    }

    @LogAno(name = "用户列表")
    @PostMapping("find")
    public Rep<SysUserListVO> findUsers(@RequestBody PageUtil.PageDTO<SysUserEntity> data){
        return Rep.ok(sysProxyService.getSysUserService().find(data));
    }

    @LogAno(name = "修改或添加用户")
    @PostMapping("edit")
    public Rep<SysUserEntity> editUser(@RequestBody SysUserEntity data){
        return Rep.ok(sysProxyService.userEdit(data));
    }

    @LogAno(name = "删除用户")
    @PostMapping("del")
    public Rep<SysUserEntity> delUser(@RequestBody SysUserEntity data){
        return Rep.ok(sysProxyService.getSysUserService().del(data));
    }

    @LogAno(name = "初始化密码")
    @PostMapping("updatePassword")
    public Rep<SysUserEntity> updateUserPassword(@RequestBody SysUserPasswordDTO data){
        return Rep.ok(sysProxyService.userInitPassword(data));
    }

    @LogAno(name = "用户信息查询")
    @PostMapping("info")
    public Rep<SysUserEntity> userInfo(){
        UserVO user = LocalUtil.getUser();
        return Rep.ok(sysProxyService.getSysUserService().info(user));
    }

    @LogAno(name = "用户修改自己信息")
    @PostMapping("updateInfo")
    public Rep<SysUserEntity> updateUserInfo(@RequestBody SysUserInfoUpdateDTO data){
        UserVO user = LocalUtil.getUser();
        return Rep.ok(sysProxyService.userUpdateInfo(data,user.getToken()));
    }


}
